import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';

import { FreightAccountService } from '../../../services/freight-account.service';

@Component({
  selector: 'app-platform-account-detail',
  templateUrl: './platform-account-detail.component.html',
  styleUrls: ['../../../../commom/less/box.less', '../../../../commom/less/expend-but.less']
})
export class PlatformAccountDetailComponent implements OnInit {
  @ViewChild('st', { static: true })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  columns: STColumn[] = this.initST();
  searchSchema: SFSchema = this.initSF();

  _$expand = false;

  info: any = {};
  params: any = {};
  constructor(public service: FreightAccountService, private route: ActivatedRoute) {
    this.params = route.snapshot.queryParams;
  }

  ngOnInit(): void {
    this.loadInfo();
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, { ...this.params });
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value
      });
      if (this.sf?.value.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf?.value?.createTime?.[0] || '',
            end: this.sf?.value?.createTime?.[1] || ''
          }
        });
      }
    }
    return requestOptions;
  };

  loadInfo() {
    this.service
      .request(this.service.$api_get_platform_account_header, {
        ...this.sf?.value,
        ...this.params,
        pageIndex: this.st.pi,
        pageSize: this.st.ps
      })
      .subscribe(res => {
        if (res) {
          this.info = res;
        }
      });
  }

  stChange(e: STChange): void {}

  exportList() {
    this.service.exportStart(
      {
        ...this.sf?.value,
        ...this.params,
        createTime: {
          start: this.sf?.value?.createTime?.[0] || '',
          end: this.sf?.value?.createTime?.[1] || ''
        },
        pageSize: -1
      },
      this.service.$api_get_exportAccountBalanceByPage
    );
  }

  goBack() {
    history.go(-1);
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this._$expand = false;
  }

  /**
   * 伸缩查询条件
   */
  expandToggle() {
    this._$expand = !this._$expand;
    this.sf?.setValue('/expand', this._$expand);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        createTime: {
          title: '交易时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择',
            nzShowTime: true
          } as SFDateWidgetSchema
        },
        channelPaySn: {
          type: 'string',
          title: '流水号',
          ui: {
            placeholder: '请输入'
          }
        },
        transactionNumber: {
          type: 'string',
          title: '交易单号',
          ui: {
            placeholder: '请输入'
          }
        },
        tradeType: {
          type: 'string',
          title: '交易类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'trade:type' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        channelSource: {
          type: 'string',
          title: '账户类型',
          enum: [
            { value: '', label: '全部' },
            { value: '1', label: '货主账户' },
            { value: '2', label: '司机账户' },
            { value: '3', label: '运营商账户' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        roleName: {
          type: 'string',
          title: '账户名称',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          }
        },
        incomeType: {
          type: 'string',
          title: '收支类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'income:type' },
            placeholder: '请选择',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        enterpriseName: {
          type: 'string',
          title: '货主',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        },
        projectName: {
          type: 'string',
          title: '所属项目',
          ui: {
            placeholder: '请输入',
            visibleIf: {
              expand: (value: boolean) => value
            }
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '交易时间', index: 'createTime', type: 'date', width: 150 },
      { title: '流水号', index: 'channelPaySn', width: 170 },
      { title: '交易类型', index: 'tradeTypeLabel', className: 'text-center', width: 130 },
      { title: '交易单号', index: 'transactionNumber', width: 190 },
      { title: '订单号', index: 'orderSn', width: 170 },
      { title: '运单号', index: 'businessNumber', width: 190 },
      { title: '账户类型', index: 'accountTypeLabel', className: 'text-center', width: 130 },
      { title: '账户名称', index: 'roleName', className: 'text-center', width: 150 },
      { title: '所属项目', index: 'projectName', className: 'text-center', width: 150 },
      { title: '收支类型', index: 'incomeTypeLabel', className: 'text-center', width: 130 },
      {
        title: '交易金额',
        index: 'amount',
        width: 150,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.amount }) }
      },
      {
        title: '账户余额',
        index: 'accountBalance',
        width: 150,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.accountBalance }) }
      },
      { title: '付款方', index: 'payName', className: 'text-center', width: 150 },
      { title: '收款方', index: 'incomeName', className: 'text-center', width: 150 },
      { title: '备注', index: 'tradeContent', className: 'text-center', width: 150 }
    ];
  }
}
